﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CSP.Bll;
using CSP.Model.CSPEnum;
using CSP.Model.CSP;

namespace CSP.Web.UserControls
{
    public partial class HomeRight : System.Web.UI.UserControl
    {
        private string _currentPagePath;

        /// <summary>
        /// 当前承载页面的路径
        /// </summary>
        public string CurrentPagePath
        {
            get
            {
                return _currentPagePath;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            _currentPagePath = this.Page.Request.Path;
        }


        /// <summary>
        /// 当程序未初始化控件时加载
        /// </summary>
        /// <param name="e"></param>
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            if (Request["HomeRight"] != null)
            {
                GetSearchData();
            }
        }

        /// <summary>
        /// 获取搜索数据
        /// </summary>
        private void GetSearchData()
        {
            string serviceAge = Request["HomeRightServiceAge"].ToString();
            string city = Request["HomeRightCity"].ToString();
            string county = Request["HomeRightCounty"].ToString();
            int areaId = 0;

            string result = "{";
            string split = "";

            List<CSP.Model.CSP.ServiceArea> serviceAreas = GetServiceArea();
            string parentId = "";
            if (city != "" && city != "北京" && city != "北京市")
            {
                ServiceArea serviceArea = serviceAreas.FirstOrDefault(s => s.Name.Contains(city));
                if (serviceArea != null)
                {
                    areaId = serviceArea.Id;
                    parentId = areaId.ToString();
                }
            }

            if (county != "")
            {
                ServiceArea serviceArea = null;
                if (parentId != "")
                {
                    serviceArea = serviceAreas.FirstOrDefault(s => s.Parentid == Convert.ToInt32(parentId) && s.Name.Contains(county));
                }
                else
                {
                    serviceArea = serviceAreas.FirstOrDefault(s => s.Name.Contains(county));
                }
                if (serviceArea != null)
                {
                    areaId = serviceArea.Id;
                }

            }

            if (serviceAge != "")
            {
                result += "\"Age\":" + GetAgeIdByAge(Convert.ToInt32(serviceAge)).ToString();
                split = ",";
            }

            if (areaId != 0)
            {
                result += split + "\"AreaId\":" + areaId.ToString();
            }
            result += "}";
            Response.Clear();
            Response.Write(result);
            Response.End();
        }

        /// <summary>
        /// 获取服务类型
        /// </summary>
        /// <returns></returns>
        private List<CSP.Model.CSP.ServiceArea> GetServiceArea()
        {
            List<CSP.Model.CSP.ServiceArea> result;
            using (var serviceAreaBll = CSPBllFactory.GetInstance().GetServiceAreaBll())
            {
                //北京
                result = serviceAreaBll.FindAll().Where(a => a.Level == 2 && a.Parentid == 383).OrderBy(s => s.Orderid).ToList();
            }
            return result;
        }

        /// <summary>
        /// 根据年龄获取年龄段ID
        /// </summary>
        /// <param name="age"></param>
        /// <returns></returns>
        private int GetAgeIdByAge(int age)
        {
            if (age >= 9 && age <= 12)
            {
                return (int)AgeGroupType.NineToTwelve;
            }
            else if (age >= 6 && age <= 9)
            {
                return (int)AgeGroupType.SixToNine;
            }
            else if (age >= 3 && age <= 6)
            {
                return (int)AgeGroupType.ThreeToSix;
            }
            else if (age >= 0 && age <= 3)
            {
                return (int)AgeGroupType.OneToThree;
            }
            else if (age >= 12 && age <= 15)
            {
                return (int)AgeGroupType.TwelveToFifteen;
            }
            else
            {
                return (int)AgeGroupType.OverFifteen;
            }
        }


    }
}